package com.fbt.transfer.app.driver.util;


import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.fbt.transfer.app.driver.R;

public class ZMToast {

  private static LayoutInflater mInflater;
  private static Toast mToast;
  private static View mView;
  private static View mViewOK;
  private static View mViewV1;

  public static void ok(Context context, String msg) {
    ok(context, msg, Toast.LENGTH_SHORT);
  }

  public static void ok(Context context, String msg, int duration) {
    try {
      cancelCurrentToast();
      mInflater = LayoutInflater.from(context);
      mView = mInflater.inflate(R.layout.toast_ok, null);
      setMsg(msg, true);
      mToast = new Toast(context);
      mToast.setView(mView);
      mToast.setDuration(duration);
      // 设置Toast显示在屏幕中间
      mToast.setGravity(Gravity.CENTER, 0, 0);
      mToast.show();
    } catch (Exception e) {
      Log.e("TAG,", "Toast.ok报错了");
    }
  }


  public static void info(Context context, String msg) {
    info(context, msg, Toast.LENGTH_SHORT);
  }

  public static void info(Context context, String msg, int duration) {
    try {
      cancelCurrentToast();
      if (mInflater == null) {
        mInflater = LayoutInflater.from(context);
      }
      mView = mInflater.inflate(R.layout.toast_info, null);
      setMsg(msg, false);
      if (mToast == null) {
        mToast = new Toast(context);
      }
      mToast.setView(mView);
      mToast.setDuration(duration);
      mToast.setGravity(Gravity.CENTER, 0, 0);
      mToast.show();
    } catch (Exception e) {
      Log.e("TAG", "Toast.info报错了", e);
    }
  }

  public static void info(Context context, String msg, boolean isOK) {
    try {
      cancelCurrentToast();
      if (mInflater == null) {
        mInflater = LayoutInflater.from(context);
      }
      mToast = new Toast(context);
      if (isOK) {
        if (mViewOK == null) {
          mViewOK = mInflater.inflate(R.layout.toast_ok, null);
        }
        TextView msg_tv = (TextView) mViewOK.findViewById(R.id.msg_tv);
        msg_tv.setText(msg);
        mToast.setView(mViewOK);
      } else {
        if (mViewV1 == null) {
          mViewV1 = mInflater.inflate(R.layout.toast_info, null);
        }
        TextView msg_tv = (TextView) mViewV1.findViewById(R.id.msg_tv);
        msg_tv.setText(msg);
        mToast.setView(mViewV1);
      }
      mToast.setDuration(Toast.LENGTH_SHORT);
      mToast.setGravity(Gravity.CENTER, 0, 0);
      mToast.show();
    } catch (Exception e) {
      Log.e("TAG,", "Toast.info报错了");
    }
  }

  private static void setMsg(String msg, boolean isOK) {
    TextView msg_tv = (TextView) mView.findViewById(R.id.msg_tv);
    msg_tv.setText(msg);
  }

  public static void cancelCurrentToast() {
    if (mToast != null) mToast.cancel();
  }

  public static void err(Context context, String message) {
    try {
      cancelCurrentToast();
      mInflater = LayoutInflater.from(context);
      mView = mInflater.inflate(R.layout.toast_info, null);
      setMsg(message, true);
      mToast = new Toast(context);
      mToast.setView(mView);
      mToast.setDuration(Toast.LENGTH_SHORT);
      // 设置Toast显示在屏幕中间
      mToast.setGravity(Gravity.CENTER, 0, 0);
      mToast.show();
    } catch (Exception e) {
      Log.e("TAG,", "Toast.ok报错了");
    }
  }
}
